// Author: Ivan Chau
package uk.ac.cam.kcic2.scene;

import java.util.ArrayList;

import uk.ac.cam.kcic2.math.Transform;

public class CompoundObject extends SceneObject {

	private ArrayList<SceneObject> children = new ArrayList<SceneObject>();

	public CompoundObject(ArrayList<Transform> trans, CompoundObject father) {
		super(trans, null, father);

		if (parent != null){
			parent.children.add(this);
		}		
	}

	public void addChild(SceneObject child){
		children.add(child);
		child.parent = this;
	}
	
	@Override
	public IntersectionInfo getIntersection(Ray ray) {
		IntersectionInfo intersection = null;
		for (SceneObject object : this.children){
			if (intersection == null){
				intersection = object.getIntersection(ray);
			}
			else if (object.getIntersection(ray) != null && intersection.t > object.getIntersection(ray).t){
				intersection = object.getIntersection(ray);
			}
		}
		return intersection;
		/*
		ArrayList<IntersectionInfo> intersections = new ArrayList<IntersectionInfo>();

		for (SceneObject child : children){
			IntersectionInfo intersection = child.getIntersection(ray);
			if (intersection != null){
				intersections.add(intersection);
			}
		}

		if (!intersections.isEmpty()){
			IntersectionInfo closest = null;
			for (IntersectionInfo intersection : intersections){
				if (closest == null){
					closest = intersection;
				}
				else if (intersection.t < closest.t){
					closest = intersection;
				}
			}
			return closest;
		}
		else{
			return null;
		}*/
	}


}
